package com.lilicould.service.impl;

import com.lilicould.dao.RepairDao;
import com.lilicould.entity.RepairRecord;
import com.lilicould.entity.RepairRequest;
import com.lilicould.service.RepairService;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class RepairServiceImpl implements RepairService {

    // 插入保修请求
    @Override
    public boolean insertRepairRequest(RepairRequest repairRequest) {
        // 生成工单号
        repairRequest.setRequestId("RQ" + System.currentTimeMillis());

        // 设置报修时间
        repairRequest.setReportDate(new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()));


        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            RepairDao repairDao = sqlSession.getMapper(RepairDao.class);
            int result = repairDao.insertRepairRequest(repairRequest);
            return result > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 插入保修记录
    @Override
    public boolean insertRepairRecord(RepairRecord repairRecord) {
        return false;
    }

    // 查询所有保修请求
    @Override
    public ArrayList<RepairRequest> selectAllRepairRequest() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            RepairDao repairDao = sqlSession.getMapper(RepairDao.class);
            return repairDao.selectAllRepairRequests();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 查询所有维修记录
    @Override
    public ArrayList<RepairRecord> selectAllRepairRecord() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            RepairDao repairDao = sqlSession.getMapper(RepairDao.class);
            return repairDao.selectAllRepairRecords();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 通过请求工单ID添加维修工
    @Override
    public boolean updateRepairEmployeeId(String requestId, String employeeId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            RepairDao repairDao = sqlSession.getMapper(RepairDao.class);
            return repairDao.updateRepairEmployeeId(requestId, employeeId) > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 完成修理请求（先删除维修请求，然后创建一条维修记录）
    @Override
    public boolean finishRepairRequest(String requestId,String content) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            RepairDao repairDao = sqlSession.getMapper(RepairDao.class);
            int deleteResult = repairDao.deleteRepairRequest(requestId);

            // 自动生成工单号
            String recordId = "RC" + System.currentTimeMillis();

            // 获取当前时间
            String repairDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
            RepairRecord repairRecord = new RepairRecord(recordId, requestId, repairDate, content);
            int insertResult = repairDao.insertRepairRecord(repairRecord);
            return deleteResult > 0 && insertResult > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
